package com.sw.rpc.service;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author sw
 * @project zdy-rpc
 * @description
 * @date 2020/5/17 20:54
 */
public class RpcDecoder extends ByteToMessageDecoder {
    private Class<?> clazz;

    private CusSerializer serializer;



    public RpcDecoder(Class<?> clazz, CusSerializer serializer) {

        this.clazz = clazz;

        this.serializer = serializer;

    }
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        Object o = serializer.deserialize(clazz, bytes);
        list.add(o);
    }
}
